package com.b.sts.controller;

import com.b.sts.entity.CarEntity;
import com.b.sts.service.CarService;
import com.b.sts.util.JwtUtil;
import com.b.sts.util.ResultUtil;
import com.b.sts.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author b
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Resource
    CarService service;

    @GetMapping("/{id}")
    public ResultVO<CarEntity> getById(@PathVariable Integer id) {
        CarEntity data = service.get(id);
        return ResultUtil.success(data);
    }

    @GetMapping("/list")
    public ResultVO<List<CarEntity>> list(CarEntity entity) {
        List<CarEntity> data = service.list(entity);
        return ResultUtil.success(data);
    }

    @GetMapping("/e/my")
    public ResultVO<List<CarEntity>> my(CarEntity entity) {
        entity.setUserId(JwtUtil.getUid());
        List<CarEntity> data = service.my(entity);
        return ResultUtil.success(data);
    }

    @PostMapping
    public ResultVO<Boolean> add(@RequestBody CarEntity entity) {
        entity.setUserId(JwtUtil.getUid());
        boolean isSuccess = service.add(entity);
        return ResultUtil.autoDo(isSuccess);
    }

    @DeleteMapping("/{id}")
    public ResultVO<Boolean> del(@PathVariable Integer id) {
        boolean isSuccess = service.del(id);
        return ResultUtil.autoDo(isSuccess);
    }

    @PutMapping
    public ResultVO<Boolean> update(@RequestBody CarEntity entity) {
        boolean isSuccess = service.update(entity);
        return ResultUtil.autoDo(isSuccess);
    }
}
